#include "cadinterface.h"
#include <QLibrary>
#include <QDebug>
#include <QString>
#include <QMessageBox>
#include <QVector3D>
#include <comdef.h>
#include <QFile>

#include "VFAdapterAPIs.h"

// 声明全局 map
std::unordered_map<std::string, std::tuple<int32_t, int32_t, int32_t, int32_t>> objectPropertiesMap;

extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGetEntityType(const char* inputValueString, char* objectType);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreatePoint(double x, double y, double z, const char*& ptID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfRemoveTargetObject(const char* TargetIntValue);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateRosette(const char* faceID, const char* pointID, const char* lineID, double startAngle, double directionAngle, double segmentLength, const char* HandDirection, const char*& RosetteID, const char*& Line1, const char*& Line2, const char*& Line3, const char*& Line4, const char*& Line5, const char*& Line6);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfSelectionToID(const wchar_t* filterType, char* intValue);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfSelectionToIDMore(const wchar_t* filterType, const char**& outputID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfChangeLineColorWidthInsertMap(const char* inputStringValue[]);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfChangeLineColorWidthFromMap(const char* inputStringValues[]);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreatePointByProjection(const char* PointID1, const char* SurfaceID, const char*& PointID2);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateCurveBySurfaceIntersection(const char* surfaceID1, const char* surfaceID2, const char*& curveID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateSplitOrTrim(const char* CurveID1, const char* CurveID2, const char* CutType, int iOrientation1, int iOrientation2);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateCurveOnSurfaceByTwoPoints(const double* X1, const double* Y1, const double* Z1, const double* X2, const double* Y2, const double* Z2, const char* SurfaceID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfStartLinkWithCAD(int iCardType);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCurveCurveIntersection(const char* lCurveTag1, const char* lCurveTag2, double& x, double& y, double& z, const char*& ptID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCurveSurfaceIntersection(const char* lCurveTag1, const char* lSurfaceTag1, double& x, double& y, double& z, const char*& ptID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfSurfaceArea(const char* surfaceID, double& dArea);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfSurfacePerimeter(const char* surfaceID, double& dPerimeter);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfSurfaceWeight(const char* surfaceID, double SurfaceDensity, double& dWeight);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateGravityOfSurface(const char* surfaceID, double& x, double& y, double& z);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCurveLength(const char* CurveID, double& Length);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfTestPointOnSurface(const char* pointID, const char* surfaceID, int& result);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfOffsetSurface(const char* surfaceID1, const char* pointID, double dis, const char*& surfaceID2);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreatPointByGeodesicDis(const char* surfaceID, const char* pointID1, double x, double y, double z, double geodesicDis, const char*& pointID2);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGetIntersectCurve(const char* CurveID, const char**& IntersectCurveID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGeodesicLengthOfPointToCurve(const char* SurfaceID, const char* CurveID, const char* PointID, double x, double y, double z, double& GeodesicLength);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreatPointByGeodesicDis2(const char* surfaceID, const char* pointID1, double x, double y, double z, double geodesicDis, double& outputx, double& outputy, double& outputz);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGetUnitTangentVectorAtPoint(const char* CurveID, const char* PointID, int bOn, double& x, double& y, double& z);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGetUnitNormalVectorAtPoint(const char* SurfaceID, const char* PointID, bool bOn, double& x, double& y, double& z);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfTestPointOnSurface2(double x, double y, double z, const char* surfaceID, int& result);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGetPointByGeodesicDis(const char* surfaceID, double x, double y, double z, double dirx, double diry, double dirz, double geodesicDis, double& outputx, double& outputy, double& outputz, int& result);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGetCoordOfPoint(const char* PointID, double& x, double& y, double& z);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfTestPointOnSurfaceByProject(const char* SurfaceID, double px, double py, double pz, double dx, double dy, double dz, int& result);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateNormalLineAndArrowhead(const char* surfaceID, const char* pointID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfDeleteNormalLineAndArrowhead(const char* pointID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfChangeSurfaceColorInsertMap(const char* inputStringValues[], const char* SurfaceID, int32_t R, int32_t G, int32_t B, const char*& LineID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfChangeSurfaceColorFromMap(const char* AssembleLineID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfChangePointColorInsertMap(const char* inputStringValue[], int32_t colorR, int32_t colorG, int32_t colorB);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfChangePointColorFromMap(const char* inputStringValue[]);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfStretchSurface(const char* SurfaceID, double Offset, const char*& ThickSurfaceID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfTestPointInEntity(const char* PointID, const char* ThickSurfaceID, bool& bOn);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateTemproraryPoint(double x, double y, double z, const char* PointID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateTemporaryPointByCAA(double x, double y, double z, int32_t colorR, int32_t colorG, int32_t colorB, const char* PointID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfTrimOrExtendCurvesFromEndPoint(const char* CurveID[], const char* SurfaceID[], double* X[], double* Y[], double* Z[], double* OffsetLength[]);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGetActiveDocumentName(BSTR& ActiveDocumentName);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfDeleteCurves(const char* Curves[]);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfOffsetCurvesBySymmetry(const char* SurfaceID, const char* CurveID, double* GeodesicDistances[], const char**& OffsetCurves);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfOffsetCurveByStartPointAndEndPoint(const char* SurfaceID, const char* CurveID, const char* Orientation, double* StartPointGeodesicDistances[], double* EndPointGeodesicDistances[], const char**& OffsetCurves);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfOffsetCurveByStartPointAndEndPointBySymmetry(const char* SurfaceID, const char* CurveID, double* StartPointGeodesicDistances[], double* EndPointGeodesicDistances[], const char**& OffsetCurves);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGetMinimumDistancePointCoordinates(const char* SurfaceID, double X1, double Y1, double Z1, double& X2, double& Y2, double& Z2);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateSpline(BSTR Name,int Plane,int length, double* X, double* Y, double* Z);
extern "C" __declspec(dllexport) VFAdapter::vfHRESULT vfGetCurveStartAndEndPointCoordinates(const char* CurveID,double& Startx, double& Starty, double& Startz,double& Endx, double& Endy, double& Endz);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGetEquidistantPointsOnCurve(const char* CurveID,int PointNums,double*& X, double*& Y, double*& Z);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfDismantleCurve(const char* CurveID, const char**& DismantlesID, const char**& LineOrCurve,int& length);
extern "C" __declspec(dllexport) VFAdapter::vfHRESULT vfGetUnitNormalVectorAtPoint2(const char* SurfaceID, double CoodX, double CoodY, double CoodZ, bool bOn, double& x, double& y, double& z);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateSpline2D(BSTR Name,int Plane,int length, double* PointCoordinate1, double* PointCoordinate2);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfDataTableSave(BSTR dbPath);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfDataTableRetrive(BSTR dbPath);
extern "C" __declspec(dllexport) VFAdapter::vfHRESULT vfGetBoundaryIDbyDivide(const char* OriginalBoundaryID, const char* SurfaceID, double px, double py, double pz, const char* DivideCurve1ID,const char* DivideCurve2ID, const char*& NewBoundaryID);
extern "C" __declspec(dllexport) VFAdapter::vfHRESULT vfCreatGeodesicCurvebyPoint(const char* BoundaryID, const char* SurfaceID, double px, double py, double pz, double dx, double dy, double dz, BSTR GeodesicCurveName, BSTR GeometricSetName,const char*& NewCurveID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfOffsetCurvesByParallel(const char* SurfaceID,const char* CurveID,double x,double y, double z,int DistanceNums,double* GeodesicDistances, BSTR GeometricSetName, BSTR* GeodesicCurvesName, const char**& OffsetCurves);
extern "C" __declspec(dllexport) VFAdapter::vfHRESULT vfTestPointAndBoundary2(const char* BoundaryID, const char* SurfaceID, double x, double y, double z, int& result);
extern "C" __declspec(dllexport) VFAdapter::vfHRESULT vfTestPointAndBoundary(double x, double y, double z,const char* surfaceID,const char* BoundaryID,int& result);
extern "C" __declspec(dllexport) VFAdapter::vfHRESULT vfCreateCurveOnSurfaceByTwoPoints2(double* X1, double* Y1, double* Z1, double* X2, double* Y2, double* Z2, const char* SurfaceID, int* R, int* G, int* B, double* Linewidth, const char**& CurveID);
extern "C" __declspec(dllexport) VFAdapter::vfHRESULT vfTestCurvesCoincidence(const char* Curve1ID, const char* Curve2ID, int& result);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGetBoundaryIDbyPoint(const char* OriginalBoundaryID, const char* SurfaceID, double ptX, double ptY, double ptZ, const char* DivideCurveID[], const char*& NewBoundaryID, const char*& NewSurfaceID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateJoin(const char* OriginalCurves[], const char*& JoinCurve);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGetNewBoundary(const char* OriginalCurveID, double x, double y, double z, const char* DivideCurveID, const char*& NewBoundaryID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGetSurfaceBoundary(const char* SurfaceID, const char*& NewBoundaryID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfSurfacesJointAndGetBoundary(const char* Surfaces[], const char*& NewBoundaryID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfTestCoincideSurface(const char* Surface1, const char* Surface2, const char*& CoincideLine, int& result);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfAddCommentOnPoint(const char* SurfaceID,double x, double y, double z,BSTR Comment,int R, int G, int B,BSTR Font,double Size,const char*& CommentID);
extern "C" __declspec(dllexport) VFAdapter::vfHRESULT vfCreateVShape(const char* SurfaceID, const char* OriginalBoundaryID,double ptX1, double ptY1, double ptZ1, double ptX2, double ptY2, double ptZ2,
                                                                     double ptX3, double ptY3, double ptZ3, const char*& NewVshapeID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateSlitDart(const char* SurfaceID, const char* OriginalBoundaryID, double d, double ptX1, double ptY1, double ptZ1, double ptX2, double ptY2, double ptZ2, const char*& NewBoundaryID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfGetPointDirSurfaceIntersections(const char* SurfaceID, double Px, double Py ,double Pz, double Dx, double Dy, double Dz, int& PointNumber, double*& X, double*& Y, double*& Z);
extern "C" __declspec(dllexport) VFAdapter::vfHRESULT VF2Create3Dsection(const char* strLaminateName, const char* strCrossSectionCurve, const char* strComponentType, const char* strBoundaryType, const char* strScaleType, double dPlyOrLayerScale, double dCoreScale, const char* strVisStyle, const char* strVisProfileType, double dMinDistance, double dCorssSectionOffset, bool bCreateCrossSectionGeometry, const char* strOutputCurveType, bool bGeometryLocked, const char* strDBPath);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfPickPointCoord(const char* SurfaceID, int PickNums, double*& X, double*& Y, double*& Z);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfImportDat(BSTR& DatPath);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateMBD(const TreeNode* rootNode1, const TreeNode* rootNode2);
extern "C" __declspec(dllexport) HRESULT vfSaveasStp(BSTR StpName, BSTR StpPath);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateLine(double x1, double y1, double z1, double x2, double y2, double z2, const char*& LineID);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfCreateSurfaceByFill(const char* Curves[], const char* Surface1, const char*& Surface2);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfOffsetSurface(const char* surfaceID1, const char* pointID, double dis, const char*& surfaceID2);
extern "C" __declspec(dllimport) VFAdapter::vfHRESULT vfSurfacesJoint(const char* Surfaces[], const char*& NewSurfaceID);

#define PRE_LOGS "[CAD_LOGS]"


CADInterface::CADInterface()
{

}

CADInterface &CADInterface::instance()
{
    static CADInterface instance;
    return instance;
}

//启动零号函数，选择CAD软件
void CADInterface::StartLinkWithCAD(int iCadType)
{
    // 调用无参数导出函数
    VFAdapter::vfHRESULT result = vfStartLinkWithCAD(iCadType);

    // 处理返回结果
    if (result == VFAdapter::vfHRESULT::vf_Success) {

        qDebug() << PRE_LOGS << "Successfully linked with CATIA application.";
        // qInfo()<< "Successfully linked with CATIA application.";

        // 后续逻辑...
    } else {
        qWarning() << PRE_LOGS << "Failed to link with CATIA application."
                   << "Error code:" << result;
    }
}
void CADInterface::fibSelectiontoID(const wchar_t* filterType,char * intValue)
{
    VFAdapter::vfHRESULT vfSelectionToIDResult = vfSelectionToID(filterType, intValue);

    if (vfSelectionToIDResult == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "函数调用成功，intValue: " << intValue;
    } else {

        qWarning() << PRE_LOGS << "函数调用失败，无法获取 intValue。"
                   << "Error code:" << vfSelectionToIDResult;
    }
}

//选择多个ID
QString  CADInterface::fibSelectionToIDMore(const wchar_t* filterType)
{
    // 定义用于接收输出的数组指针
    const char** outputID = nullptr;

    QString qsSelectIDs="";
    // 调用封装程序
    VFAdapter::vfHRESULT result = vfSelectionToIDMore(filterType, outputID);

    if (result == VFAdapter::vfHRESULT::vf_Success) {
        // 输出结果
        qDebug() << PRE_LOGS << "Selection succeeded. Strings:"<<outputID;
        for (int i = 0; outputID[i] != nullptr; ++i) {
            qsSelectIDs=qsSelectIDs+outputID[i]+",";
        }
    } else {
        qWarning() << PRE_LOGS << "Selection failed."
                   << "Error code:" << result;
    }

    return(qsSelectIDs);
}

void CADInterface::fibGetSelectiontType()
{
    const char* inputValueString = "8482328210110232831171141029799101";
    char objectType[100] = {0};
    VFAdapter::vfHRESULT vfGetEntityTypeResult = vfGetEntityType(inputValueString, objectType);

    if (vfGetEntityTypeResult == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "函数调用成功，intValue: " << objectType;
    } else {

        qWarning() << PRE_LOGS << "函数调用失败，无法获取 intValue。"
                   << "Error code:" << vfGetEntityTypeResult;
    }

    //释放内存
    delete[] inputValueString;
}


//创建array高亮接口函数
void CADInterface::fibHighLightArray(QList<QString> highLightIDs)
{
    // 输入：多个对象的整数值，以字符串数组的形式输入
    // 转换为const char**
    const char** cstrArray = new const char*[highLightIDs.size() + 1];
    for (int i = 0; i < highLightIDs.size(); ++i) {

        char* inputStringValue = new char[highLightIDs[i].length()+1];
        std::strcpy(inputStringValue,highLightIDs[i].toLocal8Bit().data());

        cstrArray[i] = inputStringValue;
    }
    cstrArray[highLightIDs.size()] = nullptr; // 添加nullptr作为数组的结束标志

    // 用于存储初始属性的变量
    int32_t lineWidth = 0;
    int32_t colorR = 0, colorG = 0, colorB = 0;

    // 调用封装的 DLL 函数
    VFAdapter::vfHRESULT result = vfChangeLineColorWidthInsertMap(cstrArray);

    // 检查结果并输出
    if (result == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "Successfully retrieved and modified the objects' properties.";
        qDebug() << PRE_LOGS << "Initial Line Width:" << lineWidth;
        qDebug() << PRE_LOGS << "Initial Color (R, G, B):" << colorR << "," << colorG << "," << colorB;
    } else {
        qWarning() << PRE_LOGS << "Failed to retrieve or modify the objects' properties."
                   << "Error code:" << result;
    }

    //释放内存
    delete[] cstrArray;
}

//创建array Cancel高亮接口函数
void CADInterface::fibunHighLightArray(QList<QString> highLightIDs)
{
    // 输入：多个对象的整数值，以字符串数组的形式输入
    // 转换为const char**
    const char** cstrArray = new const char*[highLightIDs.size() + 1];
    for (int i = 0; i < highLightIDs.size(); ++i) {
        char* inputStringValue = new char[highLightIDs[i].length()+1];
        std::strcpy(inputStringValue,highLightIDs[i].toLocal8Bit().data());

        cstrArray[i] = inputStringValue;
    }
    cstrArray[highLightIDs.size()] = nullptr; // 添加nullptr作为数组的结束标志


    // 调用封装的 DLL 函数
    VFAdapter::vfHRESULT vfSetColorLineWidthGAIResult = vfChangeLineColorWidthFromMap(cstrArray);

    // 检查结果
    if (vfSetColorLineWidthGAIResult == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "Successfully modified the line width and color of the specified objects.";
    } else {

        qWarning() << PRE_LOGS << "Failed to modify the line width and color of the specified objects."
                   << "Error code:" << vfSetColorLineWidthGAIResult;
    }

    delete[] cstrArray;
}


//创建坐标系接口函数
QString CADInterface::fibCreateRosette(QString surfaceID, QString orginID, QString directionID,
                                       double startAngle, double directionAngle, double segmentLength,
                                       QString handDirection,const char* RosetteID,
                                       const char* Line1,const char* Line2,const char* Line3,const char* Line4,const char* Line5,const char* Line6)
{
    char* faceID = new char[surfaceID.length()+1];
    std::strcpy(faceID,surfaceID.toLocal8Bit().data());

    char* pointID = new char[orginID.length()+1];
    std::strcpy(pointID,orginID.toLocal8Bit().data());

    char* lineID = new char[directionID.length()+1];
    std::strcpy(lineID,directionID.toLocal8Bit().data());


    char* handDirection_input = new char[handDirection.length()+1];
    std::strcpy(handDirection_input,handDirection.toLocal8Bit().data());


    VFAdapter::vfHRESULT vfCreateRosetteResult = vfCreateRosette(faceID, pointID, lineID,
                                                                 startAngle, directionAngle, segmentLength,handDirection_input,
                                                                 RosetteID,Line1, Line2, Line3, Line4, Line5, Line6);


    if (vfCreateRosetteResult == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "Rosette 创建成功：";
        if (RosetteID) qDebug() << PRE_LOGS << "RosetteID:" << QString::fromUtf8(RosetteID);
        if (Line1) qDebug() << PRE_LOGS << "Line1:" << QString::fromUtf8(Line1);
        if (Line2) qDebug() << PRE_LOGS << "Line2:" << QString::fromUtf8(Line2);
        if (Line3) qDebug() << PRE_LOGS << "Line3:" << QString::fromUtf8(Line3);
        if (Line4) qDebug() << PRE_LOGS << "Line4:" << QString::fromUtf8(Line4);
        if (Line5) qDebug() << PRE_LOGS << "Line5:" << QString::fromUtf8(Line5);
        if (Line6) qDebug() << PRE_LOGS << "Line6:" << QString::fromUtf8(Line6);
    } else {
        qWarning() << PRE_LOGS << "Rosette 创建失败，请检查输入的参数。"
                   << "Error code:" << vfCreateRosetteResult;
    }

    //释放内存
    delete[] faceID;
    delete[] pointID;
    delete[] lineID;
    delete[] handDirection_input;

    return(QString::fromUtf8(RosetteID));
}

//在曲面上从点(坐标)沿某一方向一定距离（测地线长度）创建点
QVector3D CADInterface::fibCreatPointByGeodesicDis(QString surfaceID,
                                                   QVector3D pointVector,
                                                   QVector3D direction,
                                                   double length)
{
    // 输入参数
    char* faceID = new char[surfaceID.length()+1];
    std::strcpy(faceID,surfaceID.toLocal8Bit().data());

    // 输出参数
    QVector3D outputVector;
    double outputX = 0.0, outputY = 0.0, outputZ = 0.0;

    // 调用 DLL 函数
    int result1;
    VFAdapter::vfHRESULT result = vfGetPointByGeodesicDis(
        faceID,  // 面的标识符
        pointVector[0], pointVector[1], pointVector[2],  // 起点坐标
        direction[0], direction[1], direction[2],   // 方向向量
        length,   // 测地距离
        outputX, outputY, outputZ,  // 输出结果
        result1);

    outputVector[0]=outputX;
    outputVector[1]=outputY;
    outputVector[2]=outputZ;
    return(outputVector);

    // 检查结果
    if (result == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "调用成功！";
        qDebug() << PRE_LOGS << "结果点坐标：";
        qDebug() << PRE_LOGS << "X =" << outputX;
        qDebug() << PRE_LOGS << "Y =" << outputY;
        qDebug() << PRE_LOGS << "Z =" << outputZ;
    } else {
        qCritical() << PRE_LOGS << "调用失败！错误代码：" << result;
    }
    //释放内存
    delete[] faceID;
}

//在曲面上从点(ID)沿某一方向一定距离（测地线长度）创建点
QVector3D CADInterface::fibCreatPointBasedIDByGeodesicDis(QString surfaceID,
                                                          QString pointID,
                                                          QVector3D direction,
                                                          double length)
{
    // 输入参数
    char* faceID = new char[surfaceID.length()+1];
    std::strcpy(faceID,surfaceID.toLocal8Bit().data());

    char* ptID = new char[pointID.length()+1];
    std::strcpy(ptID,pointID.toLocal8Bit().data());

    // 输出参数
    QVector3D outputVector;
    double outputX = 0.0, outputY = 0.0, outputZ = 0.0;

    // 调用 DLL 函数
    VFAdapter::vfHRESULT result = vfCreatPointByGeodesicDis2(
        faceID,  // 面的标识符
        ptID,  // 起点坐标
        direction[0], direction[1], direction[2],   // 方向向量
        length,   // 测地距离
        outputX, outputY, outputZ  // 输出结果
        );

    outputVector[0]=outputX;
    outputVector[1]=outputY;
    outputVector[2]=outputZ;
    return(outputVector);

    // 检查结果
    if (result == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "调用成功！";
        qDebug() << PRE_LOGS << "结果点坐标：";
        qDebug() << PRE_LOGS << "X =" << outputX;
        qDebug() << PRE_LOGS << "Y =" << outputY;
        qDebug() << PRE_LOGS << "Z =" << outputZ;
    } else {
        qCritical() << PRE_LOGS << "调用失败！错误代码：" << result;
    }

    //释放内存
    delete[] faceID;
    delete[] ptID;
}

//曲面上点到曲线的测地线距离
void CADInterface::fibGeodesicLengthOfPointToCurve(QString SurfaceID,
                                                   QString CurveID,
                                                   QString PointID,
                                                   double x, double y, double z,
                                                   double& GeodesicLength)
{
    // 输入参数
    char* surfID = new char[SurfaceID.length()+1];
    std::strcpy(surfID,SurfaceID.toLocal8Bit().data());

    char* curID = new char[CurveID.length()+1];
    std::strcpy(curID,CurveID.toLocal8Bit().data());

    char* ptID = new char[PointID.length()+1];
    std::strcpy(ptID,PointID.toLocal8Bit().data());

    GeodesicLength = 0.0;//用于返回地测线长度

    // 调用函数
    VFAdapter::vfHRESULT vfGeodesicLengthOfPointToCurveResult = vfGeodesicLengthOfPointToCurve(surfID, curID, ptID, x, y, z, GeodesicLength);

    // 检查结果并输出
    if (vfGeodesicLengthOfPointToCurveResult == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "成功计算地测线长度：";
        qDebug() << PRE_LOGS << "地测线长度为:" << GeodesicLength; // 输出地测线长度
    } else {

        qWarning() << PRE_LOGS << "计算地测线长度失败，请检查输入的参数或 CATIA 环境是否正确。"
                   << "Error code:" << vfGeodesicLengthOfPointToCurveResult;
    }

    //释放内存
    delete[] surfID;
    delete[] curID;
    delete[] ptID;
}


//曲线上点的切线方向
void CADInterface::fibGetUnitTangentVectorAtPoint(QString CurveID,
                                                  QString PointID,
                                                  bool bOn,
                                                  double& x, double& y, double& z)
{
    // 输入曲线 ID 和点 ID
    // char* curID = new char[CurveID.length()+1];
    // std::strcpy(curID,CurveID.toLocal8Bit().data());

    // char* ptID = new char[PointID.length()+1];
    // std::strcpy(ptID,PointID.toLocal8Bit().data());

    x = 0.0, y = 0.0, z = 0.0;

    // 调用动态链接库中的函数
    VFAdapter::vfHRESULT result = vfGetUnitTangentVectorAtPoint(CurveID.toLocal8Bit().data(), PointID.toLocal8Bit().data(), 0, x, y, z);

    if (result == VFAdapter::vf_Success) {
        qDebug() << PRE_LOGS << "成功获取单位切线向量:";
        qDebug() << PRE_LOGS << "X:" << x << "Y:" << y << "Z:" << z;
    } else {
        qWarning() << PRE_LOGS << "获取单位切线向量失败"
                   << "Error code:" << result;
    }

    //释放内存
    // delete[] curID;
    // delete[] ptID;
}


//曲面上点(点ID)的法向向量
void CADInterface::fibGetUnitNormalVectorAtPoint(QString SurfaceID,
                                                 QString PointID,
                                                 bool bOn,
                                                 double& x, double& y, double& z)
{
    // 输入曲面 ID 和点 ID
    char* surfID = new char[SurfaceID.length()+1];
    std::strcpy(surfID,SurfaceID.toLocal8Bit().data());

    char* ptID = new char[PointID.length()+1];
    std::strcpy(ptID,PointID.toLocal8Bit().data());

    x = 0.0, y = 0.0, z = 0.0;

    // 调用动态链接库中的函数
    VFAdapter::vfHRESULT result = vfGetUnitNormalVectorAtPoint(surfID, ptID, bOn, x, y, z);

    if (result == VFAdapter::vf_Success) {
        qDebug() << PRE_LOGS << "成功获取单位法线向量:";
        qDebug() << PRE_LOGS << "X:" << x << "Y:" << y << "Z:" << z;
    } else {
        qWarning() << PRE_LOGS << "获取单位法线向量失败"
                   << "Error code:" << result;
    }

    //释放内存
    delete[] surfID;
    delete[] ptID;
}

//曲面上点(点坐标)的法向向量
void CADInterface::fibGetUnitNormalVectorAtPointVector(QString SurfaceID,
                                                       double CoodX,
                                                       double CoodY,
                                                       double CoodZ,
                                                       bool bOn,
                                                       double& x, double& y, double& z)
{
    // 输入曲线 ID 和点坐标
    char* surfID = new char[SurfaceID.length()+1];
    std::strcpy(surfID,SurfaceID.toLocal8Bit().data());
    bOn=0;
    x = 0.0, y = 0.0, z = 0.0;

    // 调用动态链接库中的函数
    VFAdapter::vfHRESULT result = vfGetUnitNormalVectorAtPoint2(surfID, CoodX,CoodY,CoodZ, bOn, x, y, z);

    if (result == VFAdapter::vf_Success) {
        qDebug() << PRE_LOGS << "成功获取单位法线向量:";
        qDebug() << PRE_LOGS << "X:" << x << "Y:" << y << "Z:" << z;
    } else {
        qWarning() << PRE_LOGS << "获取单位法线向量失败"
                   << "Error code:" << result;
    }

    //释放内存
    delete[] surfID;
}

//根据点ID返回点坐标
QVector3D CADInterface::fibGetCoordOfPoint(QString pointID)
{
    // 输入参数
    char* ptID = new char[pointID.length()+1];
    std::strcpy(ptID,pointID.toLocal8Bit().data());

    // 输出参数
    QVector3D pointVector;
    double outputX = 0.0, outputY = 0.0, outputZ = 0.0;

    // 调用 DLL 函数
    VFAdapter::vfHRESULT result = vfGetCoordOfPoint(
        ptID,  // 点的标识符
        outputX, outputY, outputZ  // 输出结果
        );

    pointVector[0]=outputX;
    pointVector[1]=outputY;
    pointVector[2]=outputZ;

    return(pointVector);//返回结果

    // 检查结果
    if (result == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "调用成功！";
        qDebug() << PRE_LOGS << "结果点坐标：";
        qDebug() << PRE_LOGS << "X =" << outputX;
        qDebug() << PRE_LOGS << "Y =" << outputY;
        qDebug() << PRE_LOGS << "Z =" << outputZ;
    } else {
        qCritical() << PRE_LOGS << "调用失败！错误代码：" << result;
    }

    //释放内存
    delete[] ptID;
}

//判断点是否在面内，0表示不在面内，1表示在面内,2表示在边界上
int CADInterface::fibPointYNSurfFun(QString surfaceID, QVector3D pointVector)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    // 获取点到面上最近点的坐标
    double nearPoitX = 0;
    double nearPoitY = 0;
    double nearPoitZ = 0;

    // 调用封装函数（传递正确的参数类型）
    VFAdapter::vfHRESULT resultNearPoint = vfGetMinimumDistancePointCoordinates(surfID,
                                                                                pointVector.x(),
                                                                                pointVector.y(),
                                                                                pointVector.z(),
                                                                                nearPoitX,
                                                                                nearPoitY,
                                                                                nearPoitZ);


    int resultPointYNSurf = -1;                     // 用于返回点的相对位置

    // 调用函数
    VFAdapter::vfHRESULT vfTestPointOnSurface2Result = vfTestPointOnSurface2(nearPoitX, nearPoitY, nearPoitZ, surfID, resultPointYNSurf);
    // 检查函数返回值并输出结果
    if (vfTestPointOnSurface2Result == VFAdapter::vf_Success) {
        qDebug() << PRE_LOGS << "函数调用成功，点的位置判定结果：";
        if (resultPointYNSurf == 2) {
            qDebug() << PRE_LOGS << "点在边界上";
        } else if (resultPointYNSurf == 1) {
            qDebug() << PRE_LOGS << "点在面内";
        } else if (resultPointYNSurf == 0) {
            qDebug() << PRE_LOGS << "点在面外";
        } else {
            qDebug() << PRE_LOGS << "未知结果";
        }
        return(resultPointYNSurf);
    } else {

        qWarning() << PRE_LOGS << PRE_LOGS << "函数调用失败，请检查输入参数或 CATIA 环境。"
                   << "Error code:" << resultNearPoint;
    }
    //释放内存
    delete[] surfID;
}

//获得面上最近点坐标
QVector3D CADInterface::fibGetMinimumDistancePointCoordinates(QString surfaceID, QVector3D pointVector)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    // 获取点到面上最近点的坐标
    double nearPoitX = 0;
    double nearPoitY = 0;
    double nearPoitZ = 0;

    // 调用封装函数（传递正确的参数类型）
    VFAdapter::vfHRESULT resultNearPoint = vfGetMinimumDistancePointCoordinates(surfID,
                                                                                pointVector.x(),
                                                                                pointVector.y(),
                                                                                pointVector.z(),
                                                                                nearPoitX,
                                                                                nearPoitY,
                                                                                nearPoitZ);

    if (resultNearPoint == VFAdapter::vf_Success)
    {
        qDebug() << PRE_LOGS << "vfGetMinimumDistancePointCoordinates函数调用成功";
        QVector3D nearVector;
        nearVector[0]=nearPoitX;
        nearVector[1]=nearPoitY;
        nearVector[2]=nearPoitZ;
        return(nearVector);
    } else {

        qWarning() << PRE_LOGS << PRE_LOGS << "vfGetMinimumDistancePointCoordinates函数调用失败，请检查输入参数或 CATIA 环境。"
                   << "Error code:" << resultNearPoint;

    }
    //释放内存
    delete[] surfID;
}

//在面上创建两点之间的测地线
void CADInterface::fibCreateCurveOnSurfaceByTwoPoints(QVector<double> X1,
                                                      QVector<double> Y1,
                                                      QVector<double> Z1,
                                                      QVector<double> X2,
                                                      QVector<double> Y2,
                                                      QVector<double> Z2,
                                                      QString  SurfaceID)
{
    // 调用生成曲线的函数
    char* surfID = new char[SurfaceID.length()+1];
    std::strcpy(surfID,SurfaceID.toLocal8Bit().data());
    VFAdapter::vfHRESULT result = vfCreateCurveOnSurfaceByTwoPoints(X1.data(), Y1.data(), Z1.data(), X2.data(), Y2.data(), Z2.data(), surfID);

    // 检查返回结果
    if (result == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "Success! Curves created on surface successfully." ;
    } else {

        qWarning() << PRE_LOGS << PRE_LOGS << "Failure! Could not create curves on surface."
                   << "Error code:" << result;
    }
    //释放内存
    delete[] surfID;

    // //CAA虚拟特征接口
    // char* surfID = new char[SurfaceID.length()+1];
    // std::strcpy(surfID,SurfaceID.toLocal8Bit().data());

    // // int R[] = { 255,0,INT_MIN };
    // // int G[] = { 0,255,INT_MIN };
    // // int B[] = { 0,0,INT_MIN };
    // // double Linewidth[] = { 3, 5 ,NAN };

    // QVector<int> R,G,B;
    // QVector<double> Linewidth;
    // for(int i=0;i<X1.count();i++)
    // {
    //     R.append(255);
    //     G.append(255);
    //     B.append(255);
    //     Linewidth.append(3);
    // }
    // R.append(INT_MIN);
    // G.append(INT_MIN);
    // B.append(INT_MIN);
    // Linewidth.append(NAN);

    // const char** CurveID = nullptr;

    // // 调用生成曲线的函数
    // VFAdapter::vfHRESULT result = vfCreateCurveOnSurfaceByTwoPoints2(X1.data(), Y1.data(), Z1.data(), X2.data(), Y2.data(), Z2.data(), surfID, R.data(), G.data(), B.data(), Linewidth.data(), CurveID);

    // // 检查返回结果
    // if (result == VFAdapter::vfHRESULT::vf_Success) {
    //     qDebug() << PRE_LOGS << "Success! Curves created on surface successfully." ;
    // } else {

    //         qWarning() << PRE_LOGS << PRE_LOGS << "Failure! Could not create curves on surface."
    //                    << "Error code:" << result;
    //     }
}

//计算面的面积
void CADInterface::fibSurfaceArea(QString surfaceID, double& dArea)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());
    dArea = 0.0;   // 用于返回面的面积

    // 调用函数
    VFAdapter::vfHRESULT vfSurfaceAreaResult = vfSurfaceArea(surfID, dArea);

    // 检查结果并输出
    if (vfSurfaceAreaResult == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "成功获取面面积：";
        qDebug() << PRE_LOGS << "面积:" << dArea;
    } else {

        qWarning() << PRE_LOGS << PRE_LOGS << "获取面面积失败，请检查输入的参数。"
                   << "Error code:" << vfSurfaceAreaResult;
    }
    //释放内存
    delete[] surfID;
}
//计算面的周长
void CADInterface::fibSurfacePerimeter(QString surfaceID, double& dPerimeter)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());
    dPerimeter = 0.0;                  // 用于返回面的周长

    // 调用函数
    VFAdapter::vfHRESULT vfSurfacePerimeterResult = vfSurfacePerimeter(surfID, dPerimeter);

    // 检查结果并输出
    if (vfSurfacePerimeterResult == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "成功获取面周长：";
        qDebug() << PRE_LOGS << "周长:" << dPerimeter;
    } else {

        qWarning() << PRE_LOGS << "获取面周长失败，请检查输入的参数。"
                   << "Error code:" << vfSurfacePerimeterResult;
    }
    //释放内存
    delete[] surfID;
}
//计算面的重心
void CADInterface::fibCreateGravityOfSurface(QString surfaceID, double& x, double& y, double& z)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());
    x = 0.0, y = 0.0, z = 0.0;        // 用于返回面的重心坐标

    // 调用函数
    VFAdapter::vfHRESULT vfCreateGravityOfSurfaceResult = vfCreateGravityOfSurface(surfID, x, y, z);

    // 检查结果并输出
    if (vfCreateGravityOfSurfaceResult == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "成功获取面的重心坐标：";
        qDebug() << PRE_LOGS << "X:" << x << "Y:" << y << "Z:" << z; // 输出重心坐标
    } else {

        qWarning() << PRE_LOGS << "获取面的重心失败，请检查输入的参数。"
                   << "Error code:" << vfCreateGravityOfSurfaceResult;
    }

    //释放内存
    delete[] surfID;
}

//创建面的法向箭头
void CADInterface::fibCreateNormalLineAndArrowhead(QString surfaceID, QString pointID)
{
    // 输入曲面 ID 和点 ID
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    char* ptID = new char[pointID.length()+1];
    std::strcpy(ptID,pointID.toLocal8Bit().data());

    VFAdapter::vfHRESULT result = vfCreateNormalLineAndArrowhead(surfID,ptID);
    // 检查结果并输出
    if (result == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << "成功创建面的法向箭头";
    } else {
        qWarning() << PRE_LOGS << "创建面的法向箭头失败，请检查输入的参数。"
                   << "Error code:" << result;
    }

    //释放内存
    delete[] surfID;
    delete[] ptID;

}
//删除面的法向箭头
void CADInterface::fibDeleteNormalLineAndArrowhead(QString pointID)
{
    char* ptID = new char[pointID.length()+1];
    std::strcpy(ptID,pointID.toLocal8Bit().data());

    VFAdapter::vfHRESULT vfChangeLineColorWidthFromMapresult = vfDeleteNormalLineAndArrowhead(ptID);
    // 检查结果并输出
    if (vfChangeLineColorWidthFromMapresult == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << "成功删除面的法向箭头";
    } else {
        qWarning() << PRE_LOGS << "删除面的法向箭头失败，请检查输入的参数。"
                   << "Error code:" << vfChangeLineColorWidthFromMapresult;
    }
    //释放内存
    delete[] ptID;
}

//获取边界的子曲线数组,输入曲线ID，拆分曲线，输出拆所得元素的ID和几何属性
QPair<QStringList, QStringList>  CADInterface::fibGetBoundaryCurvesIdArr(QString boundaryID)
{

    QStringList curvesIDsArr;//返回子曲线id数组
    QStringList curveTypeArr;//返回子曲线类型数组
    // 准备输入参数
    char* CurveID = new char[boundaryID.length()+1];
    std::strcpy(CurveID,boundaryID.toLocal8Bit().data());

    const char** DismantlesID = nullptr;
    const char** LineOrCurve = nullptr;
    int length = 0;

    // 调用外部DLL函数
    //LineOrCurve中 Line是直线，Curve是曲线
    VFAdapter::vfHRESULT result = vfDismantleCurve(CurveID, DismantlesID,LineOrCurve,length);

    if(result == VFAdapter::vf_Success)
    {
        qDebug() << PRE_LOGS <<"DismantlesID:"<<DismantlesID[0];
        qDebug() << PRE_LOGS <<"LineOrCurve:"<<LineOrCurve[0];
        qDebug() << PRE_LOGS <<"length:"<<length;

        // 遍历直到遇到 nullptr
        for (int i = 0; i<length; ++i) {
            curvesIDsArr.append(QString::fromUtf8(DismantlesID[i]));
            curveTypeArr.append(QString::fromUtf8(LineOrCurve[i]));
        }
    }
    else
    {
        qWarning() << PRE_LOGS << "函数调用失败失败，请检查输入的参数。"
                   << "Error code:" << result;
    }
    return qMakePair(curvesIDsArr, curveTypeArr);

    //释放内存
    delete[] CurveID;
    delete[] DismantlesID;
    delete[] LineOrCurve;
}

//获取曲线类型
void CADInterface::fibGetCurveType(QString curveID,QString & curveType)
{

}

//获取获取曲线起点和终点坐标值
void CADInterface::fibGetCurveStartEndPointsVectorType(QString curveID,QVector<double> & startVec,QVector<double> & endVec)
{
    // 示例输入 CurveID，使用 std::string 方便处理
    // 1. 清空输出向量
    startVec.clear();
    endVec.clear();

    // 准备输入参数
    char* crvID = new char[curveID.length()+1];
    std::strcpy(crvID,curveID.toLocal8Bit().data());

    // 定义输出坐标变量
    double Startx = 0.0, Starty = 0.0, Startz = 0.0;
    double Endx = 0.0, Endy = 0.0, Endz = 0.0;

    // 调用DLL函数
    VFAdapter::vfHRESULT result = vfGetCurveStartAndEndPointCoordinates(
        crvID,
        Startx, Starty, Startz,
        Endx, Endy, Endz
        );

    // 检查调用结果
    if (result != VFAdapter::vf_Success) {
        qWarning() << PRE_LOGS << PRE_LOGS << "Failed to get curve points for:" << curveID
                   << "Error code:" << result;
    }

    // 填充输出向量
    startVec.reserve(3);
    endVec.reserve(3);

    startVec << Startx <<Starty <<Startz;
    endVec << Endx << Endy << Endz;

    //释放内存
    delete[] crvID;
}

//计算曲线长度
void CADInterface::fibEdgeLengthEval(QString curveID,double & curveLength)
{
    // 准备输入参数
    char* crvID = new char[curveID.length()+1];
    std::strcpy(crvID,curveID.toLocal8Bit().data());

    // 调用函数
    VFAdapter::vfHRESULT vfCurveLengthResult = vfCurveLength(crvID, curveLength);

    // 检查结果并输出
    if (vfCurveLengthResult == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << PRE_LOGS << "成功获取曲线长度：";
        qDebug() << PRE_LOGS << "长度:" << curveLength; // 输出曲线长度
    } else {

        qWarning() << PRE_LOGS << "获取曲线长度失败，请检查输入的参数。"
                   << "Error code:" << vfCurveLengthResult;
    }

    //释放内存
    delete[] crvID;
}

//获取曲线上中间多个点坐标值，相邻两点之间具有相等的测地线长度
void CADInterface::fibEdgeXyzdataEval(QString curveID,int pointsNumber,QVector<QVector3D>& pointsVectorMatrix)
{
    // 准备输入参数
    char* crvID = new char[curveID.length()+1];
    std::strcpy(crvID,curveID.toLocal8Bit().data());


    // 定义指向数据的指针变量，用于接收结果
    double* xCoordinates = nullptr;  // X 坐标数组
    double* yCoordinates = nullptr;  // Y 坐标数组
    double* zCoordinates = nullptr;  // Z 坐标数组

    // 调用外部DLL函数
    VFAdapter::vfHRESULT result = vfGetEquidistantPointsOnCurve(
        crvID,
        pointsNumber,
        xCoordinates,  // X 坐标数组（传递指针）
        yCoordinates,  // Y 坐标数组（传递指针）
        zCoordinates   // Z 坐标数组（传递指针）
        );

    if (result == VFAdapter::vf_Success) {

        // 确保 pointsVectorMatrix 有足够空间
        pointsVectorMatrix.resize(pointsNumber);
        // 存储 X Y Z坐标
        for (int i = 0; i < pointsNumber; ++i) {
            pointsVectorMatrix[i][0] = xCoordinates[i];
            pointsVectorMatrix[i][1] = yCoordinates[i];
            pointsVectorMatrix[i][2] = zCoordinates[i];
        }

    } else {

        qWarning() << PRE_LOGS << "调用函数失败，请检查输入的参数。"
                   << "Error code:" << result;
    }


    //释放内存
    delete[] crvID;
    // delete[] xCoordinates;
    // delete[] yCoordinates;
    // delete[] zCoordinates;
}

//QString转换成BSTR
BSTR CADInterface::QStringToBSTR(const QString& qstr) {
    // 1. 将QString转换为std::wstring (UTF-16)
    const std::wstring wstr = qstr.toStdWString();

    // 2. 分配BSTR内存并复制内容
    BSTR bstr = SysAllocStringLen(wstr.data(), static_cast<UINT>(wstr.length()));
    return bstr;
}

//创建在草图上的样条曲线
void CADInterface::fibCreateSpline(QString sketchName,
                                   int datumMake,
                                   QVector<double> boundaryX,
                                   QVector<double> boundaryY,
                                   QVector<double> boundaryZ)
{
    BSTR sketchNameBstr = QStringToBSTR(sketchName);
    //基准面选择 0为XY平面，1为YZ平面，2为 XZ平面

    // 根据读取的数据，动态创建指针数组
    std::vector<double> boundaryXstd(boundaryX.begin(), boundaryX.end());
    std::vector<double> boundaryYstd(boundaryY.begin(), boundaryY.end());
    std::vector<double> boundaryZstd(boundaryZ.begin(), boundaryZ.end());

    double* X = new double[boundaryXstd.size()]{0.0}; // 全部初始化为0.0
    double* Y = new double[boundaryYstd.size()]{0.0}; // 全部初始化为0.0
    double* Z = new double[boundaryZstd.size()]{0.0}; // 全部初始化为0.0

    for (size_t i = 0; i < boundaryX.count(); ++i) {
        X[i] = boundaryX.at(i);
        Y[i] = boundaryY.at(i);
        Z[i] = 0;
    }

    // 调用封装函数
    // 调用外部DLL函数创建样条曲线
    VFAdapter::vfHRESULT result = vfCreateSpline2D(sketchNameBstr, datumMake, boundaryXstd.size(), X, Y);
    if (result != VFAdapter::vf_Success) {
        qCritical() << PRE_LOGS << "创建样条曲线失败!"
                    << "Error code:"<<result;
    }

    SysFreeString(sketchNameBstr); // 必须手动释放
}

//数据库内容保存到part模型中
void CADInterface::fibDataTableSave(QString databasePath)
{
     BSTR databasePathBstr = QStringToBSTR(databasePath);

    VFAdapter::vfHRESULT result = vfDataTableSave(databasePathBstr);
    if (result != VFAdapter::vf_Success) {
        qCritical() <<  PRE_LOGS << "保存失败!"
            << "Error code:"<<result;
        return;
    }
}

//part模型中复材相关内容保存到数据库
void CADInterface::fibDataTableRetrive(QString databasePath)
{
    BSTR databasePathBstr = QStringToBSTR(databasePath);

    VFAdapter::vfHRESULT result = vfDataTableRetrive(databasePathBstr);
    if (result != VFAdapter::vf_Success) {

        qCritical() << PRE_LOGS << "提取失败!"
            << "Error code:"<<result;
        return ;
    }
}

//绘制点到边界测底线线段
QString CADInterface::fibCreatGeodesicCurvebyPoint(QString SurfaceID,
                                                QString BoundaryID,
                                                double x1,double y1,double z1,
                                                double x2,double y2,double z2,
                                                QString GeodesicCurveName,
                                                QString GeometricSetName)
{
    // 准备输入参数
    char* boundID = new char[BoundaryID.length()+1];
    std::strcpy(boundID,BoundaryID.toLocal8Bit().data());

    char* surfID = new char[SurfaceID.length()+1];
    std::strcpy(surfID,SurfaceID.toLocal8Bit().data());

    BSTR GeodesicCurveName1 = QStringToBSTR(GeodesicCurveName);
    BSTR GeometricSetName1 = QStringToBSTR(GeometricSetName);

    const char* NewBoundaryID = nullptr;

    qDebug() << "输入" <<SurfaceID<<","<<BoundaryID<<","<<x1<<","<<y1<<","<<z1<<",";
    qDebug() << "输入" <<x2<<","<<y2<<","<<z2<<","<<GeodesicCurveName<<","<<GeometricSetName<<",";


    // 调用函数
    VFAdapter::vfHRESULT Result = vfCreatGeodesicCurvebyPoint(boundID,surfID,x1,y1,z1,x2,y2,z2,GeodesicCurveName1,GeometricSetName1,NewBoundaryID);


    // 检查结果并输出
    if (Result == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << "边界ID:" << QString::fromUtf8(NewBoundaryID);
        return QString::fromUtf8(NewBoundaryID);  // Convert const char* to QString
    } else {
        qDebug() << "Failed to create point, error code:" << Result;
        return QString();  // Return empty QString on failure
    }
}

//根据点坐标创建点，并返回点id
QString CADInterface::fibCreatePoint(double x, double y, double z)
{
    // Define ptID as const char* and initialize to nullptr
    const char* ptID = nullptr;

    // Call vfCreatePoint (assuming it will allocate/fill ptID)
    VFAdapter::vfHRESULT createPointResult = vfCreatePoint(x, y, z, ptID);

    // Check result
    if (createPointResult == VFAdapter::vf_Success && ptID != nullptr) {
        qDebug() << "Point created successfully, ptID:" << ptID;
        return QString::fromUtf8(ptID);  // Convert const char* to QString
    } else {
        qDebug() << "Failed to create point, error code:" << createPointResult;
        return QString();  // Return empty QString on failure
    }
}

//分割线与原始边界形成新的边界环
QString CADInterface::fibGetBoundaryIDbyDivide(QString boundaryID,
                                               QString suefaceID,
                                               QString divideCurveID1,
                                               QString divideCurveID2,
                                               double px,
                                               double py,
                                               double pz)
{
    // 准备输入参数
    char* boundID = new char[boundaryID.length()+1];
    std::strcpy(boundID,boundaryID.toLocal8Bit().data());

    char* surfID = new char[suefaceID.length()+1];
    std::strcpy(surfID,suefaceID.toLocal8Bit().data());

    char* divCurID1 = new char[divideCurveID1.length()+1];
    std::strcpy(divCurID1,divideCurveID1.toLocal8Bit().data());

    char* divCurID2 = new char[divideCurveID2.length()+1];
    std::strcpy(divCurID2,divideCurveID2.toLocal8Bit().data());

    // 调用函数
    const char* NewBoundaryID = nullptr;
    VFAdapter::vfHRESULT Result = vfGetBoundaryIDbyDivide(boundID, surfID, px, py, pz, divCurID1, divCurID2,NewBoundaryID);

    //qDebug() << "交点ID:" << QString::fromUtf8(pointID);

    // 检查结果并输出
    if (Result == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << "边界ID:" << QString::fromUtf8(NewBoundaryID);
        return QString::fromUtf8(NewBoundaryID);  // Convert const char* to QString
    } else {
        qDebug() << "Failed to create point, error code:" << Result;
        return QString();  // Return empty QString on failure
    }
}

QStringList CADInterface::convertToQStringList(const char** offsetCurves) {
    QStringList result;

    if (!offsetCurves) return result; // handle null input

    for (int i = 0; offsetCurves[i] != nullptr; ++i) {
        result.append(QString::fromUtf8(offsetCurves[i]));
    }

    return result;
}

//平行偏移生成多条曲线
QStringList CADInterface::fibOffsetCurvesByParallel(QString surfaceID,
                                                    QString curveID,
                                                    double x,
                                                    double y,
                                                    double z,
                                                    int DistanceNums,
                                                    QStringList GeodesicCurvesName,
                                                    QString GeometricSetName,
                                                    QVector<double >geodesicDistances)
{
    // 准备输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    char* crvID = new char[curveID.length()+1];
    std::strcpy(crvID,curveID.toLocal8Bit().data());

    double* doubleArray = new double[geodesicDistances.size()];
    std::copy(geodesicDistances.begin(), geodesicDistances.end(), doubleArray);

    BSTR GeometricSetName1 = QStringToBSTR(GeometricSetName);


    // 偏置曲线名称数组
    // Ensure sizes match
    assert(DistanceNums == GeodesicCurvesName.count());

    BSTR* GeodesicCurvesName1 = new BSTR[DistanceNums];
    for(int i = 0; i < DistanceNums; i++)
    {
        GeodesicCurvesName1[i] = QStringToBSTR(GeodesicCurvesName.at(i));
    }


    // 输出的偏置曲线（初始化为 nullptr）
    const char** offsetCurves = nullptr;

    // 调用封装函数（传递正确的参数类型）
    VFAdapter::vfHRESULT result = vfOffsetCurvesByParallel(
        surfID,
        crvID,
        x,
        y,
        z,
        DistanceNums,
        doubleArray, // 传递 double* 数组
        GeometricSetName1,
        GeodesicCurvesName1,
        offsetCurves
        );

    // Cleanup
    for(int i = 0; i < DistanceNums; i++)
    {
        SysFreeString(GeodesicCurvesName1[i]);
    }
    delete[] GeodesicCurvesName1;

    QStringList qCurves = convertToQStringList(offsetCurves);


    // 检查结果并输出
    if (result == VFAdapter::vfHRESULT::vf_Success) {
        for (size_t i = 0; offsetCurves[i] != nullptr; ++i) {
            qDebug() << "Offset Curve ID [" << i + 1 << "] :" << offsetCurves[i];
        }
        return qCurves;  // Convert const char* to QString
    } else {
        qDebug() << "Failed to create point, error code:" << result;
    }
}


//判断点是否在边界内，0表示点在边界外，1表示点在边界内,2表示点在边界上，其他表示点在面外
int CADInterface::fibPointYNBoundaryFun(QString surfaceID, QString boundaryID, QVector3D pointVector)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    char* boundID = new char[boundaryID.length()+1];
    std::strcpy(boundID,boundaryID.toLocal8Bit().data());

    // 获取点到面上最近点的坐标
    double nearPoitX = 0;
    double nearPoitY = 0;
    double nearPoitZ = 0;


    // 调用封装函数（传递正确的参数类型）
    VFAdapter::vfHRESULT resultNearPoint = vfGetMinimumDistancePointCoordinates(surfID,
                                                                                pointVector.x(),
                                                                                pointVector.y(),
                                                                                pointVector.z(),
                                                                                nearPoitX,
                                                                                nearPoitY,
                                                                                nearPoitZ);

    // 调用函数
    int result = -1;  // 用于返回点与边界的关系（0：界外，1：界内，2：界上）
    VFAdapter::vfHRESULT vfTestPointOnBoundaryResult = vfTestPointAndBoundary(nearPoitX,
                                                                              nearPoitY,
                                                                              nearPoitZ,
                                                                              surfID,
                                                                              boundID,
                                                                              result);
    // VFAdapter::vfHRESULT vfTestPointOnBoundaryResult = vfTestPointAndBoundary2(
    //     boundID,
    //     surfID,
    //     nearPoitX,
    //     nearPoitY,
    //     nearPoitZ,
    //     result);
    // 检查结果并输出
    if (vfTestPointOnBoundaryResult == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << "函数调用成功，点的位置判定结果：";
        if (result == 2) {
            qDebug() << "点在边界上";
        } else if (result == 1) {
            qDebug() << "点在边界内";
        } else if (result == 0) {
            qDebug() << "点在边界外";
        } else {
            qDebug() << "点在面外";
        }

        return result;
    } else {
        qDebug() << "函数调用失败，请检查输入参数或 CATIA 环境是否正确。";

        return -1;
    }

}

//判断曲线是否重合,1曲线重合，0曲线不重合，-1曲线不重合,-2函数调用失败
int CADInterface::fibCurvesCoincidence(QString curve1ID,QString curve2ID)
{
    // 输入参数
    char* Curve1ID = new char[curve1ID.length()+1];
    std::strcpy(Curve1ID,curve1ID.toLocal8Bit().data());

    char* Curve2ID = new char[curve2ID.length()+1];
    std::strcpy(Curve2ID,curve2ID.toLocal8Bit().data());

    int result = -2;  // 用于返回点与面的关系（0：不重合，1：重合，-1：曲线不存在）

    // 调用函数
    VFAdapter::vfHRESULT vfTestCurvesCoincidenceResult = vfTestCurvesCoincidence(Curve1ID, Curve2ID, result);

    // 检查结果并输出
    if (vfTestCurvesCoincidenceResult == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << "函数调用成功，点的位置判定结果：";
        if (result == 0) {
            qDebug() << "曲线不重合";
        }
        else if (result == 1) {
            qDebug() << "曲线重合";
        }
        else if (result == -1) {
            qDebug() << "曲线不存在";
        }
        else {
            qDebug() << "未知结果";
        }

        return(result);
    }
    else {
        qDebug() << "函数调用失败，请检查输入参数或 CATIA 环境是否正确。";
        return(-2);
    }
}

//合并曲线，返回曲线ID
QString CADInterface::fibCombileCurves(QStringList curvesIDs)
{
    const char** originalCurves = new const char*[curvesIDs.size() + 1];
    for (int i = 0; i < curvesIDs.size(); ++i) {
        char* inputStringValue = new char[curvesIDs[i].length()+1];
        std::strcpy(inputStringValue,curvesIDs[i].toLocal8Bit().data());

        originalCurves[i] = inputStringValue;
    }
    originalCurves[curvesIDs.size()]=nullptr;

    const char* JoinCurveID = nullptr;

    VFAdapter::vfHRESULT result = vfCreateJoin(originalCurves, JoinCurveID);

    // 检查结果并输出
    if (result == VFAdapter::vfHRESULT::vf_Success) {
        qDebug() << "接合线ID为：" << JoinCurveID;
        qDebug() << "返回值为：" << result;
        return QString::fromUtf8(JoinCurveID);
    }
    else {
        qDebug() << "接合失败";
        qDebug() << "返回值为：" << result;
        return QString();
    }

}
//边界及内部曲线共同构建环，并且返回环的ID
QPair<QString,QString> CADInterface::fibGetCircleIDbyPoint(QString surfaceID,
                                            QString boundaryID,
                                            double x,
                                            double y,
                                            double z,
                                            QStringList zoneBoundariesIDs)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    char* boundID = new char[boundaryID.length()+1];
    std::strcpy(boundID,boundaryID.toLocal8Bit().data());

    // 转换为const char**
    const char** DividedCurvesIDs = new const char*[zoneBoundariesIDs.size() + 1];
    for (int i = 0; i < zoneBoundariesIDs.size(); ++i) {
        char* inputStringValue = new char[zoneBoundariesIDs[i].length()+1];
        std::strcpy(inputStringValue,zoneBoundariesIDs[i].toLocal8Bit().data());

        DividedCurvesIDs[i] = inputStringValue;
    }
    DividedCurvesIDs[zoneBoundariesIDs.size()] = nullptr; // 添加nullptr作为数组的结束标志

    const char* NewBoundaryID = nullptr;
    const char* NewSurfaceID = nullptr;

    // 调用调用vfGetBoundaryIDbyPoint函数
    VFAdapter::vfHRESULT result = vfGetBoundaryIDbyPoint(boundID, surfID, x, y, z, DividedCurvesIDs, NewBoundaryID,NewSurfaceID);

    // 检查结果并输出
    if (result == VFAdapter::vfHRESULT::vf_Success) {
        return qMakePair(QString::fromUtf8(NewSurfaceID),QString::fromUtf8(NewBoundaryID));  // Convert const char* to QString

    }
    else {
        qDebug() << "调用失败" ;
        throw std::runtime_error("函数调用失败，请检查输入参数或 CATIA 环境是否正确。");
    }
}

//判断面是否相邻,1面相邻，0面不相邻，-1非法输入,并输出相邻面的共线段id
QPair<int, QString> CADInterface::fibSurfacesAdjacent(QString surface1ID,QString surface2ID)
{
    char* surf1ID = new char[surface1ID.length()+1];
    std::strcpy(surf1ID,surface1ID.toLocal8Bit().data());

    char* surf2ID = new char[surface2ID.length()+1];
    std::strcpy(surf2ID,surface2ID.toLocal8Bit().data());

    const char* CoincideLine = nullptr;
    int adjacentResult = -1;

    // 调用封装函数（传递正确的参数类型）
    VFAdapter::vfHRESULT Result = vfTestCoincideSurface(surf1ID, surf2ID,CoincideLine,adjacentResult);
    // 检查结果并输出
    if (Result == VFAdapter::vfHRESULT::vf_Success) {
        return qMakePair(adjacentResult, QString::fromUtf8(CoincideLine));
    } else {
        throw std::runtime_error("函数调用失败，请检查输入参数或 CATIA 环境是否正确。");
    }
}

//多个曲面接合成为新的曲面，并且返回新曲面的边界环
QString CADInterface::fibSurfacesJointAndGetBoundary(QStringList surfaceList)
{
    // 转换为const char**
    const char** surfaces = new const char*[surfaceList.size() + 1];
    for (int i = 0; i < surfaceList.size(); ++i) {
        char* inputStringValue = new char[surfaceList[i].length()+1];
        std::strcpy(inputStringValue,surfaceList[i].toLocal8Bit().data());

        surfaces[i] = inputStringValue;
    }
    surfaces[surfaceList.size()] = nullptr; // 添加nullptr作为数组的结束标志

    const char* NewBoundaryID = nullptr;
    // const char* NewSurfaceID = nullptr;

    // 调用外部DLL函数
    VFAdapter::vfHRESULT result = vfSurfacesJointAndGetBoundary( surfaces,NewBoundaryID);

    // 检查结果并输出
    if (result == VFAdapter::vfHRESULT::vf_Success) {
        return(NewBoundaryID);
    }
    else {
        qDebug() << "vfSurfacesJointAndGetBoundary调用失败";
        throw std::runtime_error("vfSurfacesJointAndGetBoundary调用失败");
    }
}

//分隔原有边界，获取新边界
QString CADInterface::fibGetNewBoundary(QString originalCurveID,
                                        double x,
                                        double y,
                                        double z,
                                        QString dividedCurveID)
{
    char* originalCrvID = new char[originalCurveID.length()+1];
    std::strcpy(originalCrvID,originalCurveID.toLocal8Bit().data());

    char* dividedCrvID = new char[dividedCurveID.length()+1];
    std::strcpy(dividedCrvID,dividedCurveID.toLocal8Bit().data());

    const char* NewBoundaryID = nullptr;

    // 调用vfCreateSlitDart函数
    VFAdapter::vfHRESULT result = vfGetNewBoundary(originalCrvID, x, y, z, dividedCrvID, NewBoundaryID);

    // 检查结果并输出
    if (result == VFAdapter::vfHRESULT::vf_Success) {
        return QString::fromUtf8(NewBoundaryID);  // Convert const char* to QString
    }
    else {
        qDebug() << "创建失败";
        throw std::runtime_error("vfGetNewBoundary调用失败");
    }
}

//在点上增加平行屏幕的注释特征
QString CADInterface::fibAddCommentOnPoint(double x,
                                        double y,
                                        double z,
                                        QString commentStr,
                                        int R,
                                        int G,
                                        int B,
                                        QString font,
                                        double wordSize,
                                        QString surfaceID)
{
    // 准备输入参数
    char* surf1ID = new char[surfaceID.length()+1];
    std::strcpy(surf1ID,surfaceID.toLocal8Bit().data());

    BSTR Comment = QStringToBSTR(commentStr);
    BSTR Font = QStringToBSTR(font);
    const char* CommentID = nullptr;

    // 调用封装函数（传递正确的参数类型）
    VFAdapter::vfHRESULT result = vfAddCommentOnPoint(
        surf1ID,
        x,
        y,
        z,
        Comment,
        R,
        G,
        B,
        Font,
        wordSize,
        CommentID
        );

    if (result == VFAdapter::vf_Success) {
        qDebug() << "创建注释成功";
        return QString::fromUtf8(CommentID);  // Convert const char* to QString
    }
    else{
        qDebug() << "创建注释失败";
        throw std::runtime_error("vfAddCommentOnPoint调用失败");
    }

    SysFreeString(Comment);
    SysFreeString(Font);
}

//创建V型剪口特征
QString CADInterface::fibCreateVShapeDart(QString surfaceID,
                                       QString boundaryID,
                                       double ptX1,double ptY1,double ptZ1,
                                       double ptX2,double ptY2,double ptZ2,
                                       double ptX3,double ptY3,double ptZ3)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    char* boundID = new char[boundaryID.length()+1];
    std::strcpy(boundID,boundaryID.toLocal8Bit().data());

    const char* NewBoundaryID = nullptr;  // 用于接收返回的循环ID

    // 调用函数 vfCreateVShapeDart
    VFAdapter::vfHRESULT result = vfCreateVShape(surfID,
                                                 boundID,
                                                 ptX1, ptY1, ptZ1,
                                                 ptX2, ptY2, ptZ2,
                                                 ptX3, ptY3, ptZ3,
                                                 NewBoundaryID);

    // 检查结果并输出
    if (result == VFAdapter::vf_Success) {
        qDebug() << "成功执行，返回的 NewBoundaryID: " << QString::fromUtf8(NewBoundaryID);
        return QString::fromUtf8(NewBoundaryID);  // Convert const char* to QString

    }
    else {
        qDebug() << "执行失败！" ;
        throw std::runtime_error("vfCreateVShapeDart调用失败");
    }
}

//创建缝型剪口特征,
//d为缝宽度尺寸
QString CADInterface::fibCreateSlitDart(QString surfaceID,
                                        QString boundaryID,
                                        double d,
                                        double ptX1,double ptY1,double ptZ1,
                                        double ptX2,double ptY2,double ptZ2)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    char* boundID = new char[boundaryID.length()+1];
    std::strcpy(boundID,boundaryID.toLocal8Bit().data());

    const char* NewBoundaryID = nullptr;  // 用于接收返回的循环ID

    // 调用函数 vfCreateVShapeDart
    VFAdapter::vfHRESULT result = vfCreateSlitDart(surfID,
                                                   boundID,
                                                   d,
                                                   ptX1, ptY1, ptZ1,
                                                   ptX2, ptY2, ptZ2,
                                                   NewBoundaryID);

    // 检查结果并输出
    if (result == VFAdapter::vf_Success) {
        qDebug() << "成功执行，返回的 NewBoundaryID: " << QString::fromUtf8(NewBoundaryID);
        return QString::fromUtf8(NewBoundaryID);  // Convert const char* to QString

    }
    else {
        qDebug() << "执行失败！" ;
        throw std::runtime_error("vfCreateSlitDart调用失败");
    }
}

//选择面上多个点坐标，可以鼠标单击面上任一点也可以选择面上已存在的点
void CADInterface::fibSelectSrfPoints(QString surfaceID,
                                      int pointsNum,
                                      QVector<QVector<double>>& selectPointsCorrd)
{
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    // 定义指针变量用于接收输出坐标
    double* xCoordinates = nullptr;  // X 坐标数组
    double* yCoordinates = nullptr;  // Y 坐标数组
    double* zCoordinates = nullptr;  // Z 坐标数组

    // 调用外部DLL函数
    VFAdapter::vfHRESULT result = vfPickPointCoord(
        surfID,
        pointsNum,
        xCoordinates,  // X 坐标数组（传递指针）
        yCoordinates,  // Y 坐标数组（传递指针）
        zCoordinates   // Z 坐标数组（传递指针）
        );

    if (result == VFAdapter::vf_Success) {
        // 确保 crossPoints 有足够空间
        // selectPointsCorrd.resize(pointsNum);
        // 存储 X Y Z坐标
        for (int i = 0; i < pointsNum; ++i) {
            QVector<double> xyzCoor;
            xyzCoor.append(xCoordinates[i]);
            xyzCoor.append(yCoordinates[i]);
            xyzCoor.append(zCoordinates[i]);
            selectPointsCorrd.append(xyzCoor);
        }

    } else {
        qDebug() << "调用函数fibSelectSrfPoints失败，错误代码：" << result;
    }
}

//导入dat文件，生成catia模型的网格
void CADInterface::fibImportDatGrid(QString& datPath)
{
    // 输入参数：示例的 CurveRingID 和 PointNum;
    BSTR DatPath = SysAllocString(L"");  // 初始化 BSTR

    // 调用外部DLL函数
    VFAdapter::vfHRESULT result = vfImportDat(DatPath);

    if (result == VFAdapter::vf_Success) {
        qDebug() << "调用函数成功：" << result;
        // 将 BSTR 转换为 QString
        datPath = QString::fromWCharArray(DatPath);
        datPath.replace("\\\\", "\\");  // 将多余的 '\\' 转换为一个 '\'
        qDebug() << "路径：" << datPath;

    } else {
        qDebug() << "调用函数失败，错误代码：" << result;
    }
}

//生成stp
void CADInterface::fibSaveAsStp(QString stpName,QString stpPath)
{
    // 将 QString 转换为 BSTR
    BSTR bstrStpName = SysAllocString(stpName.toStdWString().c_str());
    BSTR bstrStpPath = SysAllocString(stpPath.toStdWString().c_str());

    // 调用 vfSaveasStp 函数
    HRESULT result = vfSaveasStp(bstrStpName, bstrStpPath);

    if (SUCCEEDED(result)) {
        qDebug() << "STP 文件保存成功。";
    } else {
        qDebug() << "保存 STP 文件失败。";
    }

    // 释放 BSTR 内存
    SysFreeString(bstrStpName);
    SysFreeString(bstrStpPath);
}

//生成MBD
void CADInterface::fibCreateMBD(std::unique_ptr<TreeNode> rootNode1, std::unique_ptr<TreeNode> rootNode2)
{
    if (!rootNode1 || !rootNode2) {
        qDebug() << "错误：节点指针为空";
        return;
    }

    // 释放所有权，将指针交给 DLL
    VFAdapter::vfHRESULT result = vfCreateMBD(rootNode1.get(), rootNode2.get());

    if (result == VFAdapter::vf_Success) {
        qDebug() << "调用函数成功：" << result;
    } else {
        qDebug() << "调用函数失败，错误代码：" << result;
        // 注意：如果失败，内存可能已泄漏
    }
}


fibHRESULT CADInterface::fibGetEntityType(long lID, geom_entity_type& enType)
{
    return fibHRESULT();
}

fibHRESULT CADInterface::fibCreatePoint(double x, double y, double z, long& ptID)
{
    return fibHRESULT();
}

fibHRESULT CADInterface::fibCurveCurveIntersection(long lCurveTag1, long lCurveTag2, double& x, double& y, double& z, bool bCreatePoint, long& ptID)
{
    return fibHRESULT();
}

fibHRESULT CADInterface::fibCurveSurfaceIntersection(long lCurveTag1, long lSurfaceTag1, double& x, double& y, double& z, bool bCreatePoint, long& ptID)
{
    return fibHRESULT();
}

fibHRESULT CADInterface::fibSelectSingleByInteractive(geom_entity_type geoType, long& tagID, char* strMessage, bool bObjectActionParadigmPossibility)
{
    return fibHRESULT();
}

fibHRESULT CADInterface::fibSelectByTag(int tagID)
{
    return fibHRESULT();
}

fibHRESULT CADInterface::fibClearSeletion()
{
    return fibHRESULT();
}

fibHRESULT CADInterface::fibOffsetCurve(long curveID1, long surfaceID, double dis, long& curveID2)
{
    return fibHRESULT();
}

fibHRESULT CADInterface::fibCreateCurveBySurfaceIntersection(long surfaceID1, long surfaceID2, long& curveID)
{
    return fibHRESULT();
}

void CADInterface::fibTestPointAndBoundary(const QVector3D& point, const QString& surfaceID, const QString& boundaryID, int& mark)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    char* boundID = new char[boundaryID.length()+1];
    std::strcpy(boundID,boundaryID.toLocal8Bit().data());

    mark = -1;

    // 调用 DLL 函数
    VFAdapter::vfHRESULT result = vfTestPointAndBoundary(
        point.x(), point.y(), point.z(),
        surfID, boundID, mark);

    if (result == VFAdapter::vf_Success) {
        qDebug() << PRE_LOGS << "成功判断点与边界的位置关系:";
        qDebug() << PRE_LOGS << "Mark:" << mark;
    } else {
        qWarning() << PRE_LOGS << "判断点与边界的位置关系失败"
                   << "Error code:" << result;
    }

    //释放内存
    delete[] surfID;
    delete[] boundID;
}

void CADInterface::fibGetPointDirSurfaceIntersections(const QString& surfaceID,
                                        const QVector3D& point,
                                        const QVector3D& pointVector,
                                        QVector<QVector3D>& crossPoints)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    // 定义指向数据的指针变量，用于接收结果
    int pointsNumber = -1;
    double* xCoordinates = nullptr;  // X 坐标数组
    double* yCoordinates = nullptr;  // Y 坐标数组
    double* zCoordinates = nullptr;  // Z 坐标数组

    // 调用 DLL 函数
    VFAdapter::vfHRESULT result = vfGetPointDirSurfaceIntersections(
        surfID,
        point.x(), point.y(), point.z(),
        pointVector.x(), pointVector.y(), pointVector.z(),
        pointsNumber,
        xCoordinates,  // X 坐标数组（传递指针）
        yCoordinates,  // Y 坐标数组（传递指针）
        zCoordinates   // Z 坐标数组（传递指针）
        );

    if (result == VFAdapter::vf_Success) {
        // 确保 crossPoints 有足够空间
        crossPoints.resize(pointsNumber);
        // 存储 X Y Z坐标
        for (int i = 0; i < pointsNumber; ++i) {
            crossPoints[i][0] = xCoordinates[i];
            crossPoints[i][1] = yCoordinates[i];
            crossPoints[i][2] = zCoordinates[i];
        }
    } else {
        qWarning() << PRE_LOGS << "调用函数失败，请检查输入的参数。"
                   << "Error code:" << result;
    }

    //释放内存
    delete[] surfID;
    // delete[] xCoordinates;
    // delete[] yCoordinates;
    // delete[] zCoordinates;
}

void CADInterface::fibCreate3Dsection(const QString& laminateID,
                              const QString& crossSectionCurveID,
                              const QString& componentType,
                              const QString& boundaryType,
                              const QString& scaleType,
                              const double& plyOrLayerScale,
                              const double& coreScale,
                              const QString& visableStyle,
                              const QString& visableProfileType,
                              const double& minDistance,
                              const double& corssSectionOffset,
                              const bool& createCrossSectionGeometry,
                              const QString& outputCurveType,
                              const bool& geometryLocked,
                              const QString& dbPath)
{
    // 输入参数
    char* strLaminateName = new char[laminateID.length()+1];
    std::strcpy(strLaminateName,laminateID.toLocal8Bit().data());

    char* strCrossSectionCurve = new char[crossSectionCurveID.length()+1];
    std::strcpy(strCrossSectionCurve,crossSectionCurveID.toLocal8Bit().data());

    char* strComponentType = new char[componentType.length()+1];
    std::strcpy(strComponentType,componentType.toLocal8Bit().data());

    char* strBoundaryType = new char[boundaryType.length()+1];
    std::strcpy(strBoundaryType,boundaryType.toLocal8Bit().data());

    char* strScaleType = new char[scaleType.length()+1];
    std::strcpy(strScaleType,scaleType.toLocal8Bit().data());

    char* strVisStyle = new char[visableStyle.length()+1];
    std::strcpy(strVisStyle,visableStyle.toLocal8Bit().data());

    char* strVisProfileType = new char[visableProfileType.length()+1];
    std::strcpy(strVisProfileType,visableProfileType.toLocal8Bit().data());

    char* strOutputCurveType = new char[outputCurveType.length()+1];
    std::strcpy(strOutputCurveType,outputCurveType.toLocal8Bit().data());

    char* strDBPath = new char[dbPath.length()+1];
    std::strcpy(strDBPath,dbPath.toLocal8Bit().data());

    // 调用 DLL 函数
    VFAdapter::vfHRESULT result = VF2Create3Dsection(strLaminateName,
                                                     strCrossSectionCurve,
                                                     strComponentType,
                                                     strBoundaryType,
                                                     strScaleType,
                                                     plyOrLayerScale,
                                                     coreScale,
                                                     strVisStyle,
                                                     strVisProfileType,
                                                     minDistance,
                                                     corssSectionOffset,
                                                     createCrossSectionGeometry,
                                                     strOutputCurveType,
                                                     geometryLocked,
                                                     strDBPath
                                                     );

    if (result == VFAdapter::vf_Success) {
        qDebug() << PRE_LOGS << "成功创建 3D Cross Section";
    } else {
        qWarning() << PRE_LOGS << "调用函数失败，请检查输入的参数。"
                   << "Error code:" << result;
    }

    //释放内存
    delete[] strLaminateName;
    delete[] strCrossSectionCurve;
    delete[] strComponentType;
    delete[] strBoundaryType;
    delete[] strScaleType;
    delete[] strVisStyle;
    delete[] strVisProfileType;
    delete[] strOutputCurveType;
    delete[] strDBPath;
}

// 提取边界
QString CADInterface::fibGetSurfaceBoundary(const QString& surfaceID)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    // 输出参数
    const char* bdID = nullptr;

    // 调用 DLL 函数
    VFAdapter::vfHRESULT result = vfGetSurfaceBoundary(surfID, bdID);

    // Check result
    if (result == VFAdapter::vf_Success && bdID != nullptr) {
        qDebug() << "Successfully to get surface boundary, bdID:" << bdID;
        return QString::fromUtf8(bdID);  // Convert const char* to QString
    } else {
        qDebug() << "Failed to get surface boundary, error code:" << result;
        return QString();  // Return empty QString on failure
    }

    //释放内存
    delete[] surfID;
    delete[] bdID;
}

// 多点选择
void CADInterface::fibPickPointCoord(const QString& surfaceID, int& pickNumber, QVector<QVector3D>& points, int& errorMark)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    // 定义指向数据的指针变量，用于接收结果
    double* xCoordinates = nullptr;  // X 坐标数组
    double* yCoordinates = nullptr;  // Y 坐标数组
    double* zCoordinates = nullptr;  // Z 坐标数组

    // 调用 DLL 函数
    VFAdapter::vfHRESULT result = vfPickPointCoord(
        surfID,
        pickNumber,
        xCoordinates,  // X 坐标数组（传递指针）
        yCoordinates,  // Y 坐标数组（传递指针）
        zCoordinates   // Z 坐标数组（传递指针）
        );

    if (result == VFAdapter::vf_Success) {
        // 确保 points 有足够空间
        points.resize(pickNumber);
        // 存储 X Y Z坐标
        for (int i = 0; i < pickNumber; ++i) {
            points[i][0] = xCoordinates[i];
            points[i][1] = yCoordinates[i];
            points[i][2] = zCoordinates[i];
        }
        errorMark = 1;
    } else {
        qWarning() << PRE_LOGS << "调用函数失败，请检查输入的参数。"
                   << "Error code:" << result;
        errorMark = 0;
    }

    //释放内存
    delete[] surfID;
    delete[] xCoordinates;
    delete[] yCoordinates;
    delete[] zCoordinates;
}

// 创建直线
void CADInterface::fibCreateLine(const QVector3D& startPoint, const QVector3D& endPoint, QString& lineID)
{
    // 输入参数
    const char* strlineID = nullptr;

    // 调用 DLL 函数
    VFAdapter::vfHRESULT result = vfCreateLine(
        startPoint.x(),
        startPoint.y(),
        startPoint.z(),
        endPoint.x(),
        endPoint.y(),
        endPoint.z(),
        strlineID
        );

    if (result == VFAdapter::vf_Success) {
        qDebug() << PRE_LOGS << "Line 创建成功：";
    } else {
        qWarning() << PRE_LOGS << "调用函数失败，请检查输入的参数。"
                   << "Error code:" << result;
    }

    lineID = QString::fromUtf8(strlineID);

    //释放内存
    delete[] strlineID;
}

// 曲线填充
void CADInterface::fibCreateSurfaceByFill(const QString& surfaceID, const QStringList& curvesIDs, QString& filledSurfaceID)
{
    // 输入参数
    char* strSurfID = new char[surfaceID.length()+1];
    std::strcpy(strSurfID,surfaceID.toLocal8Bit().data());

    const char** originalCurves = new const char*[curvesIDs.size() + 1];
    for (int i = 0; i < curvesIDs.size(); ++i) {
        char* inputStringValue = new char[curvesIDs[i].length()+1];
        std::strcpy(inputStringValue,curvesIDs[i].toLocal8Bit().data());

        originalCurves[i] = inputStringValue;
    }
    originalCurves[curvesIDs.size()]=nullptr;

    // 输出参数
    const char* strResultID = nullptr;

    // 调用 DLL 函数
    VFAdapter::vfHRESULT result = vfCreateSurfaceByFill(
        originalCurves,
        strSurfID,
        strResultID
        );

    if (result == VFAdapter::vf_Success) {
        qDebug() << PRE_LOGS << "Fill Surface 创建成功：";
    } else {
        qWarning() << PRE_LOGS << "调用函数失败，请检查输入的参数。"
                   << "Error code:" << result;
    }

    filledSurfaceID = QString::fromUtf8(strResultID);

    //释放内存
    delete[] strSurfID;
    for (int i = 0; i < curvesIDs.size() + 1; ++i) {
        delete[] const_cast<char*>(originalCurves[i]);
    }
    delete[] originalCurves;
    originalCurves = nullptr;
    delete[] strResultID;
}


void CADInterface::fibTestPointOnSurface(const QString& pointID, const QString& surfaceID, int& mark)
{
    // 若返回0，则点在面外；若返回1，则点在面内；若返回2，则点在面的边界上

    // 输入参数
    char* ptID = new char[pointID.length()+1];
    std::strcpy(ptID,pointID.toLocal8Bit().data());

    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    // 调用 DLL 函数
    VFAdapter::vfHRESULT result = vfTestPointOnSurface(ptID, surfID, mark);

    if (result == VFAdapter::vf_Success) {
        qDebug() << PRE_LOGS << "成功判断点与边界的位置关系:";
        qDebug() << PRE_LOGS << "Mark:" << mark;
    } else {
        qWarning() << PRE_LOGS << "判断点与边界的位置关系失败"
                   << "Error code:" << result;
    }

    //释放内存
    delete[] surfID;
    delete[] ptID;
}

// 曲面偏移
void CADInterface::fibOffsetSurface(const QString& surfaceID, const QString& pointID, const double& distance, QString& resultID)
{
    // 输入参数
    char* surfID = new char[surfaceID.length()+1];
    std::strcpy(surfID,surfaceID.toLocal8Bit().data());

    char* ptID = new char[pointID.length()+1];
    std::strcpy(ptID,pointID.toLocal8Bit().data());

    // 输出参数
    const char* resID = nullptr;

    // 调用 DLL 函数
    VFAdapter::vfHRESULT result = vfOffsetSurface(surfID, ptID, distance, resID);

    if (result == VFAdapter::vf_Success && resID != nullptr) {
        qDebug() << "Successfully to offset surface, resultID:" << resID;
        resultID = QString::fromUtf8(resID);
    } else {
        qDebug() << "Failed to offset surface, error code:" << result;
        resultID = QString();
    }

    //释放内存
    delete[] surfID;
    delete[] ptID;
    delete[] resID;
}

// 曲面合并
void CADInterface::fibSurfacesJoint(const QStringList& surfacesIDs, QString& jointSurfaceID)
{
    // 输入参数
    const char** originalSurfaces = new const char*[surfacesIDs.size() + 1];
    for (int i = 0; i < surfacesIDs.size(); ++i) {
        char* inputStringValue = new char[surfacesIDs[i].length()+1];
        std::strcpy(inputStringValue,surfacesIDs[i].toLocal8Bit().data());

        originalSurfaces[i] = inputStringValue;
    }
    originalSurfaces[surfacesIDs.size()]=nullptr;

    // 输出参数
    const char* strResultID = nullptr;

    // 调用 DLL 函数
    VFAdapter::vfHRESULT result = vfSurfacesJoint(
        originalSurfaces,
        strResultID
        );

    if (result == VFAdapter::vf_Success) {
        qDebug() << PRE_LOGS << "Fill Surface 创建成功：";
    } else {
        qWarning() << PRE_LOGS << "调用函数失败，请检查输入的参数。"
                   << "Error code:" << result;
    }

    jointSurfaceID = QString::fromUtf8(strResultID);

    //释放内存
    for (int i = 0; i < surfacesIDs.size() + 1; ++i) {
        delete[] const_cast<char*>(originalSurfaces[i]);
    }
    delete[] originalSurfaces;
    originalSurfaces = nullptr;
    delete[] strResultID;
}
